package com.cskaoyan.controller.admin.mallcontroller;

import com.cskaoyan.bo.mallbo.KeyWordBo;
import com.cskaoyan.service.mallservice.KeyWordService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.mallvo.KeyWordListVo;
import com.cskaoyan.vo.mallvo.KeyWordVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author hsp
 * @date 2021/5/10 21:27
 */
@RestController
@RequestMapping("admin/keyword")
public class KeyWordController {
    @Autowired
    KeyWordService keyWordService;

    @RequiresPermissions("admin:keyword:list")
    @RequestMapping("list")
    public BaseRespVo list(Integer page,Integer limit,String keyword,String url,String sort,String order){

       KeyWordListVo keyWordListVo =  keyWordService.listKeyWord(page,limit,keyword,url,sort,order);

        return BaseRespVo.ok(keyWordListVo);
    }

    @RequiresPermissions("admin:keyword:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Map map)
    {
        String keyword = (String) map.get("keyword");
        String url = (String) map.get("url");
        Boolean isHot = (Boolean) map.get("isHot");
        Integer isHotInt = isHot?1:0;
        Boolean isDefault = (Boolean) map.get("isDefault");
        Integer isDefaultInt = isDefault?1:0;
        KeyWordVo keyWordVo =  keyWordService.createKeyWord(keyword,url,isHotInt,isDefaultInt);


        return BaseRespVo.ok(keyWordVo);
    }

    @RequiresPermissions("admin:keyword:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody KeyWordBo keyWordBo)
    {
       KeyWordBo keyWordBoVo =  keyWordService.updateKeyWord(keyWordBo);

       return BaseRespVo.ok(keyWordBoVo);
    }

    @RequiresPermissions("admin:keyword:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        Integer id = (Integer) map.get("id");
        keyWordService.logicDeleteKeyWord(id);
        return BaseRespVo.ok();
    }
}
